package com.investment.advisor.controller;

import com.investment.advisor.dto.PortfolioDTO;
import com.investment.advisor.dto.StrategyDTO;
    import com.investment.advisor.service.PortfolioService;
import com.investment.advisor.service.StrategyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/test")
@Api(tags = "测试接口", description = "用于测试StackOverflowError修复")
public class TestController {

    private final PortfolioService portfolioService;
    private final StrategyService strategyService;

    @Autowired
    public TestController(PortfolioService portfolioService, StrategyService strategyService) {
        this.portfolioService = portfolioService;
        this.strategyService = strategyService;
    }

    @GetMapping("/portfolios")
    @ApiOperation("测试获取所有组合")
    public ResponseEntity<Map<String, Object>> testGetAllPortfolios() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<PortfolioDTO> portfolios = portfolioService.getAllPortfolios();
            result.put("success", true);
            result.put("message", "成功获取所有组合，没有发生StackOverflowError");
            result.put("count", portfolios.size());
            return ResponseEntity.ok(result);
        } catch (StackOverflowError e) {
            result.put("success", false);
            result.put("error", "StackOverflowError");
            result.put("message", "发生堆栈溢出错误: " + e.getMessage());
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", e.getClass().getSimpleName());
            result.put("message", "发生错误: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    @GetMapping("/strategies")
    @ApiOperation("测试获取所有策略")
    public ResponseEntity<Map<String, Object>> testGetAllStrategies() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<StrategyDTO> strategies = strategyService.getAllStrategies();
            result.put("success", true);
            result.put("message", "成功获取所有策略，没有发生StackOverflowError");
            result.put("count", strategies.size());
            return ResponseEntity.ok(result);
        } catch (StackOverflowError e) {
            result.put("success", false);
            result.put("error", "StackOverflowError");
            result.put("message", "发生堆栈溢出错误: " + e.getMessage());
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", e.getClass().getSimpleName());
            result.put("message", "发生错误: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    @GetMapping("/simple")
    @ApiOperation("简单测试 - 不调用复杂查询")
    public ResponseEntity<Map<String, Object>> simpleTest() {
        Map<String, Object> result = new HashMap<>();
        try {
            // 创建一个简单的对象来测试JSON序列化
            Map<String, Object> testData = new HashMap<>();
            testData.put("message", "这是一个简单的测试");
            testData.put("timestamp", System.currentTimeMillis());
            
            result.put("success", true);
            result.put("message", "简单测试成功");
            result.put("data", testData);
            return ResponseEntity.ok(result);
        } catch (StackOverflowError e) {
            result.put("success", false);
            result.put("error", "StackOverflowError");
            result.put("message", "发生堆栈溢出错误: " + e.getMessage());
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", e.getClass().getSimpleName());
            result.put("message", "发生错误: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    @GetMapping("/health")
    @ApiOperation("健康检查")
    public ResponseEntity<Map<String, Object>> healthCheck() {
        Map<String, Object> result = new HashMap<>();
        result.put("status", "UP");
        result.put("message", "应用程序运行正常");
        return ResponseEntity.ok(result);
    }
} 